﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYCodeBase.WinForm;
using HYCodeBase.XmlUtility;

namespace ElectricDim
{
	public partial class DlgWireLoopDim : DlgModelLessCanClose
	{
		public static DlgWireLoopDim GetInstance(UIApplication app, WindowHandle handel)
		{
			if (DlgWireLoopDim.dll == null || DlgWireLoopDim.dll.IsDisposed)
			{
				DlgWireLoopDim.dll = new DlgWireLoopDim(app, handel);
			}
			return DlgWireLoopDim.dll;
		}

		public Autodesk.Revit.DB.ElementType GetElementType()
		{
			Family labelFamily = ElectricMark.WireLoopMark(this.mApp.ActiveUIDocument.Document)[0].LoadInSubTrans(this.mApp.ActiveUIDocument.Document);
			return ElectricalUtils.GetLeaderType(this.mApp.ActiveUIDocument.Document, this.GetDimLinkStyle(), labelFamily);
		}

		public LabelType GetLabelType()
		{
			if (this.rbFromWire.Checked)
			{
				return LabelType.FromObject;
			}
			return LabelType.FromText;
		}

		public string Label
		{
			get
			{
				return this.hyLoopNumber1.StrCode;
			}
			set
			{
				this.hyLoopNumber1.StrCode = value;
			}
		}

		public LeaderStyle GetDimStyle()
		{
			if (this.rbNullLeader.Checked)
			{
				return LeaderStyle.None;
			}
			if (this.rbHasLeader.Checked)
			{
				return LeaderStyle.HasLeader;
			}
			return LeaderStyle.Orthogonal;
		}

		public PointStyle GetDimLinkStyle()
		{
			if (this.rbNull.Checked)
			{
				return PointStyle.None;
			}
			return PointStyle.Line;
		}

		public DlgWireLoopDim(UIApplication app, WindowHandle handle) : base(handle)
		{
			this.mApp = app;
			this.InitializeComponent();
			this.SetEnableTextAndButton();
		}

		private void rbNullLeader_CheckedChanged(object sender, EventArgs e)
		{
			this.groupBox3.Enabled = !this.rbNullLeader.Checked;
		}

		private void rbFromWire_CheckedChanged(object sender, EventArgs e)
		{
			this.SetEnableTextAndButton();
		}

		private void SetEnableTextAndButton()
		{
			this.hyLoopNumber1.IsEnable = !this.rbFromWire.Checked;
			this.rbFreeLabel.Enabled = this.hyLoopNumber1.IsEnable;
			this.rbAutoAdd1.Enabled = this.hyLoopNumber1.IsEnable;
		}

		private void DlgWireLoopDim_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveDlgData();
		}

		private void DlgWireLoopDim_Load(object sender, EventArgs e)
		{
			try
			{
				this.xmlHelper.LoadXml(this.path, "Root");
			}
			catch (Exception)
			{
				File.Delete(this.path);
			}
			try
			{
				this.LoadDlgData();
			}
			catch
			{
			}
		}

		public void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYConnectWire", "Params"));
			string attribute = this.xmlHelper.GetAttribute("FromWire");
			if (!string.IsNullOrEmpty(attribute))
			{
				this.rbFromWire.Checked = bool.Parse(attribute);
			}
			string attribute2 = this.xmlHelper.GetAttribute("Input");
			if (!string.IsNullOrEmpty(attribute2))
			{
				this.rbInput.Checked = bool.Parse(attribute2);
			}
			string attribute3 = this.xmlHelper.GetAttribute("LoopNumber");
			if (string.IsNullOrEmpty(attribute3))
			{
				this.hyLoopNumber1.StrCode = "WL1";
			}
			else
			{
				this.hyLoopNumber1.StrCode = attribute3;
			}
			string attribute4 = this.xmlHelper.GetAttribute("FreeLabel");
			if (!string.IsNullOrEmpty(attribute4))
			{
				this.rbFreeLabel.Checked = bool.Parse(attribute4);
			}
			string attribute5 = this.xmlHelper.GetAttribute("AutoAdd1");
			if (!string.IsNullOrEmpty(attribute5))
			{
				this.rbAutoAdd1.Checked = bool.Parse(attribute5);
			}
			string attribute6 = this.xmlHelper.GetAttribute("NullLeader");
			if (!string.IsNullOrEmpty(attribute6))
			{
				this.rbNullLeader.Checked = bool.Parse(attribute6);
			}
			string attribute7 = this.xmlHelper.GetAttribute("HasLeader");
			if (!string.IsNullOrEmpty(attribute7))
			{
				this.rbHasLeader.Checked = bool.Parse(attribute7);
			}
			string attribute8 = this.xmlHelper.GetAttribute("OrtLeader");
			if (!string.IsNullOrEmpty(attribute8))
			{
				this.rbOrtLeader.Checked = bool.Parse(attribute8);
			}
			string attribute9 = this.xmlHelper.GetAttribute("Null");
			if (!string.IsNullOrEmpty(attribute9))
			{
				this.rbNull.Checked = bool.Parse(attribute9);
			}
			string attribute10 = this.xmlHelper.GetAttribute("Line");
			if (!string.IsNullOrEmpty(attribute10))
			{
				this.rbLine.Checked = bool.Parse(attribute10);
			}
		}

		public void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYConnectWire"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYConnectWire", "Params"));
			this.xmlHelper.SetAttribute("FromWire", this.rbFromWire.Checked.ToString());
			this.xmlHelper.SetAttribute("Input", this.rbInput.Checked.ToString());
			this.xmlHelper.SetAttribute("LoopNumber", this.hyLoopNumber1.StrCode);
			this.xmlHelper.SetAttribute("FreeLabel", this.rbFreeLabel.Checked.ToString());
			this.xmlHelper.SetAttribute("AutoAdd1", this.rbAutoAdd1.Checked.ToString());
			this.xmlHelper.SetAttribute("NullLeader", this.rbNullLeader.Checked.ToString());
			this.xmlHelper.SetAttribute("HasLeader", this.rbHasLeader.Checked.ToString());
			this.xmlHelper.SetAttribute("OrtLeader", this.rbOrtLeader.Checked.ToString());
			this.xmlHelper.SetAttribute("Null", this.rbNull.Checked.ToString());
			this.xmlHelper.SetAttribute("Line", this.rbLine.Checked.ToString());
			this.xmlHelper.SaveXml("");
		}

		private void hyLoopNumber1_UserControlTextValueChanged(object sender, EventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			WireConnectForm.wireParams.LoopNumber = textBox.Text;
		}

		public string AutoAddLoopNum(string strCode, int iStep)
		{
			return this.hyLoopNumber1.GetLoopNum(strCode, iStep);
		}

		private HYCodeBase.XmlUtility.XmlHelper xmlHelper = new HYCodeBase.XmlUtility.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "WireLoopDim.xml");

		private const string Root = "Root";

		private const string Node = "HYConnectWire";

		private const string ChildNode = "Params";

		private const string FromWire = "FromWire";

		private const string Input = "Input";

		private const string LoopNumber = "LoopNumber";

		private const string FreeLabel = "FreeLabel";

		private const string AutoAdd1 = "AutoAdd1";

		private const string NullLeader = "NullLeader";

		private const string HasLeader = "HasLeader";

		private const string OrtLeader = "OrtLeader";

		private const string Null = "Null";

		private const string Line = "Line";

		private static DlgWireLoopDim dll;

		private UIApplication mApp;
	}
}
